package org.jeecg.modules.demo.xxljob;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.policy.PictureRenderPolicy;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class test {


    public static void main(String[] args) {
        Map<String, Object> datas = new HashMap<>();

        datas.put("title", "项目到期授权情况");
        datas.put("totalAmount", 5000.00);
        datas.put("srcPic", new PictureRenderData(100, 100, "D:\\opt\\upFiles\\temp\\02_1667184598543.jpg"));

        List<String> pictureTag = new ArrayList<>();
        pictureTag.add("D:\\opt\\upFiles\\temp\\02_1667184598543.jpg");

        List<Map<String, Object>> listMap = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            Map<String, Object> lm = new HashMap<String, Object>();
            lm.put("itemName", "项目" + i);
            lm.put("amount", "2" + i);
            lm.put("authorizationDeadline", "2022-11-01");
            lm.put("hardwareAmount", "2022-11-01");
            lm.put("hardWareAuthorizationDeadlineStr", "2022-11-02");
            listMap.add(lm);
        }
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        Configure config = Configure.builder().bind("lists", policy).build();//设置列表配置，如果有多个列表时需加.bind("list1", policy) 新列表配置即可
        for (String tag : pictureTag
        ) {
            config.customPolicy(tag, new PictureRenderPolicy());//设置图片，不然保存的是一串字符
        }
        datas.put("lists", listMap);        //将列表保存到渲染的map中
        //3.创建XWPFTemplate对象，并设置读取模板路径和要渲染的数据
        XWPFTemplate template = XWPFTemplate.compile("D:\\opt\\template\\软件运维模板.xlsx", config).render(datas);
        //compile(模板路径,对应的配置)方法是设置模板路径和模板配置的，如果不设置配置时可不传config
        //render(datas)方法是用来渲染数据，将准备好的map数据方进去渲染
        //4.模板的输出，用FileOutputStream输出流（可以输出到指定文件位置，也可以用ajax直接返回给浏览器下载）
        FileOutputStream out = null;//创建文件输出流并指定位置
        try {
            out = new FileOutputStream("D:\\opt\\poi\\result2.xlsx");
            template.write(out);
            out.flush();
            out.close();
            template.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

